package org.agile4j.rpc.ngbc.testcase;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.agile4j.rpc.client.RpcProxy;
import org.agile4j.rpc.ngbc.pojo.Person;
import org.agile4j.rpc.ngbc.service.HelloService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring.xml")
public class HelloTestCase {
	private static final Logger LOGGER = LoggerFactory.getLogger(HelloTestCase.class);

    @Autowired
    private RpcProxy rpcProxy;

    @Test
    public void test01() {
        HelloService helloService = rpcProxy.create(HelloService.class);
        String result = helloService.hello("World");
        LOGGER.debug(result);

        HelloService helloService2 = rpcProxy.create(HelloService.class, "ngbc.hello2");
        String result2 = helloService2.hello("世界");
        LOGGER.debug(result2);

        System.exit(0); // 0--正常结束程序 ;1--异常关闭程序；
    }

    @Test
    public void test02() {
        HelloService helloService = rpcProxy.create(HelloService.class);
        String result = helloService.hello(new Person("Yongxu", "Han"));
        LOGGER.debug(result);

        System.exit(0);
    }

    @Test
    public void test03(){
        int loopCount = 100;

        long start = System.currentTimeMillis();

        HelloService helloService = rpcProxy.create(HelloService.class);
        for (int i = 0; i < loopCount; i++) {
            String result = helloService.hello("World");
            LOGGER.debug(result);
        }

        long time = System.currentTimeMillis() - start;
        LOGGER.debug("loop: " + loopCount);
        LOGGER.debug("time: " + time + "ms");
        LOGGER.debug("tps: " + (double) loopCount / ((double) time / 1000));

        System.exit(0);
    }

    @Test
    public void test04() {
        int threadNum = 10;
        int loopCount = 100;

        ExecutorService executor = Executors.newFixedThreadPool(threadNum);
        final CountDownLatch latch = new CountDownLatch(loopCount);

        try {
            long start = System.currentTimeMillis();

            for (int i = 0; i < loopCount; i++) {
                executor.submit(new Runnable() {
                    @Override
                    public void run() {
                        HelloService helloService = rpcProxy.create(HelloService.class);
                        String result = helloService.hello("World");
                        LOGGER.debug(result);
                        latch.countDown();
                    }
                });
            }
            latch.await();

            long time = System.currentTimeMillis() - start;
            LOGGER.debug("thread: " + threadNum);
            LOGGER.debug("loop: " + loopCount);
            LOGGER.debug("time: " + time + "ms");
            LOGGER.debug("tps: " + (double) loopCount / ((double) time / 1000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            executor.shutdown();
        }

        System.exit(0);
    }
}
